#include "Search_Result.h"
#include "_LDIF.h"


Search_Result::Search_Result(char *ras,bool s,vector<Atribut<char *>> atrib_cautate):LDAP_Result(ras,s),atribute_cautate(atrib_cautate)
{
	
}

Search_Result::Search_Result (char *ras,bool s):LDAP_Result(ras,s)
{

}

Search_Result::~Search_Result(void)
{
}

void Search_Result::afisareResult()
{
	if(Succes==false)
		throw new Search_Result_Exception("",12);


	vector<Atribut<char *>> atr_gasite;

	if(atribute_cautate.size())
	{
		if(atribute_cautate.size()>1)
			cout<<"\n->Atributele cautate sunt:\n";
		else
			cout<<"\n->Atributul cautat este:\n";

		for(int i=0;i<atribute_cautate.size();i++)
		{
			cout<<atribute_cautate[i].daNume()<<endl;
		}


		printf("\n->Rezultatul operatiei de cautare este: \n");

		char *p=NULL,sep[]=":\n";

		Atribut<char *>a,a1;
		vector<Atribut<char *>> atribut;

		p=strtok(raspuns,sep);
		while(p)
		{
			a.setNume(p);
			p=strtok(NULL,sep);
			a.setValue(p+1);
			p=strtok(NULL,sep);
			atribut.push_back(a);
		}


		for(int i=0;i<atribute_cautate.size();i++)
			for(int j=0;j<atribut.size();j++)
			{
				if(!strcmp(atribut[j].daNume(),atribute_cautate[i].daNume()))
				{
					atr_gasite.push_back(atribut[j]);
					printf("\nNume atribut: %s",atribut[j].daNume());
					printf("\nValoare atribut: %s",atribut[j].daAtribut());
				}
			}
	}
	else
	{
		printf("\n->Intrarea cautata contine urmatoarele atribute: \n");
		printf("\n%s\n",raspuns);

		char *p=NULL,sep[]=":\n";

		Atribut<char *>a;

		p=strtok(raspuns,sep);
		while(p)
		{
			a.setNume(p);
			p=strtok(NULL,sep);
			a.setValue(p+1);
			p=strtok(NULL,sep);
			atr_gasite.push_back(a);
		}
	}

	if(atr_gasite.size())
	{
		_LDIF ldif_modify(atr_gasite);
		

		char ldif[100];
		strcpy(ldif,ldif_modify.Da_LDIF());

		this->ldif_Result = new char[strlen(ldif)+1];
		strcpy(ldif_Result,ldif);
		printf("\n->Acest rezultat a fost salvat in fisierul %s",ldif_Result);
	}
	else
	{
		printf("\n->Nu s-a gasit nici un atribut in intrarea respectiva...");
	}
		
}

vector<Atribut<char *>> Search_Result::Da_atribute_cautate()const
{
	return atribute_cautate;
}


